package threedee.generator;

import processing.core.*;

import java.util.Random;


public class BuildingBlock extends AbstractBlock{
	
	private int height;  //height of building, 1-5.
	private BlockType type; // type of building, 0-2.
	private Random r = new Random();
	
	// constructor which randomizes height
	public BuildingBlock(BlockType type){
		this.height = this.r.nextInt(5) + 1;
		this.type = type;
	}
	
	public BuildingBlock(){
		this.height = this.r.nextInt(5) + 1;
		switch (this.r.nextInt(2)) {
		case 0: 	this.type = BlockType.BUILDING1;
					break;
		case 1:		this.type = BlockType.BUILDING2;
					break;
		}
		
		 
	}
	
	public int getRotation(){
		return 0;
	}
	
	public int getHeight(){
		return this.height;
	}
	
	public BlockType getType(){
		return this.type;
	}

	public void draw(PGraphics g, PImage[] texture, float x, float y){
		g.pushMatrix();
		g.translate(x*BLOCK_SCALE-BLOCK_SCALE/2, y*BLOCK_SCALE-BLOCK_SCALE/2, 0);
		this.drawBuilding(g, texture);
		g.popMatrix();
		
	}
	
	private void drawBuilding(PGraphics g, PImage[] texture){
		g.noStroke();
		switch (this.type) {
		//draw type 1 building
		case BUILDING1: 
			drawSkyscraper(g, texture[0], texture[2]);
			break;
						
		//draw type 2 building
		case BUILDING2: 			
			drawSkyscraper(g, texture[1], texture[3]);
						break;
		default:		break;
					
		}
	}
	
	private void drawSkyscraper(PGraphics g, PImage textureSide, PImage textureTop){
		int u = textureSide.width;
		int v = textureSide.height;
		
		g.beginShape();
		g.texture(textureSide);
		g.vertex(0, 0, 0, 0, v);
		g.vertex(0, BLOCK_SCALE, 0, u, v);
		g.vertex(0, BLOCK_SCALE, this.height*BLOCK_SCALE, u, 0);
		g.vertex(0, 0, this.height*BLOCK_SCALE, 0, 0);
		g.endShape();
		
		g.beginShape();
		g.texture(textureSide);
		g.vertex(0, 0, 0, 0, v);
		g.vertex(BLOCK_SCALE, 0, 0, u, v);
		g.vertex(BLOCK_SCALE, 0, this.height*BLOCK_SCALE, u, 0);
		g.vertex(0, 0, this.height*BLOCK_SCALE, 0, 0);
		g.endShape();

		g.beginShape();
		g.texture(textureSide);
		g.vertex(BLOCK_SCALE, 0, 0, 0, v);
		g.vertex(BLOCK_SCALE, BLOCK_SCALE, 0, u, v);
		g.vertex(BLOCK_SCALE, BLOCK_SCALE, this.height*BLOCK_SCALE, u, 0);
		g.vertex(BLOCK_SCALE, 0, this.height*BLOCK_SCALE, 0, 0);
		g.endShape();
		
		g.beginShape();
		g.texture(textureSide);
		g.vertex(0, BLOCK_SCALE, 0, 0, v);
		g.vertex(BLOCK_SCALE, BLOCK_SCALE, 0, u, v);
		g.vertex(BLOCK_SCALE, BLOCK_SCALE, this.height*BLOCK_SCALE, u, 0);
		g.vertex(0, BLOCK_SCALE, this.height*BLOCK_SCALE, 0, 0);
		g.endShape();
		
		//draw skyscraper top
		
		g.beginShape();
		g.texture(textureTop);
		g.vertex(0, 0, this.height*BLOCK_SCALE, 0, 0);
		g.vertex(0, BLOCK_SCALE, this.height*BLOCK_SCALE, u, 0);
		g.vertex(0, BLOCK_SCALE, this.height*BLOCK_SCALE+BLOCK_SCALE/10, u, v);
		g.vertex(0, 0, this.height*BLOCK_SCALE+BLOCK_SCALE/10, 0, v);
		g.endShape();
		
		g.beginShape();
		g.texture(textureTop);
		g.vertex(0, 0, this.height*BLOCK_SCALE, 0, 0);
		g.vertex(BLOCK_SCALE, 0, this.height*BLOCK_SCALE, u, 0);
		g.vertex(BLOCK_SCALE, 0, this.height*BLOCK_SCALE+BLOCK_SCALE/10, u, v);
		g.vertex(0, 0, this.height*BLOCK_SCALE+BLOCK_SCALE/10, 0, v);
		g.endShape();

		g.beginShape();
		g.texture(textureTop);
		g.vertex(BLOCK_SCALE, 0, this.height*BLOCK_SCALE, 0, 0);
		g.vertex(BLOCK_SCALE, BLOCK_SCALE, this.height*BLOCK_SCALE, u, 0);
		g.vertex(BLOCK_SCALE, BLOCK_SCALE, this.height*BLOCK_SCALE+BLOCK_SCALE/10, u, v);
		g.vertex(BLOCK_SCALE, 0, this.height*BLOCK_SCALE+BLOCK_SCALE/10, 0, v);
		g.endShape();
		
		g.beginShape();
		g.texture(textureTop);
		g.vertex(0, BLOCK_SCALE, this.height*BLOCK_SCALE, 0, 0);
		g.vertex(BLOCK_SCALE, BLOCK_SCALE, this.height*BLOCK_SCALE, u, 0);
		g.vertex(BLOCK_SCALE, BLOCK_SCALE, this.height*BLOCK_SCALE+BLOCK_SCALE/10, u, v);
		g.vertex(0, BLOCK_SCALE, this.height*BLOCK_SCALE+BLOCK_SCALE/10, 0, v);
		g.endShape();
		
		g.beginShape();
		g.texture(textureTop);
		g.vertex(0, 0, this.height*BLOCK_SCALE+BLOCK_SCALE/10, 0, 0);
		g.vertex(0, BLOCK_SCALE, this.height*BLOCK_SCALE+BLOCK_SCALE/10, u, 0);
		g.vertex(BLOCK_SCALE, BLOCK_SCALE, this.height*BLOCK_SCALE+BLOCK_SCALE/10, u, v);
		g.vertex(BLOCK_SCALE, 0, this.height*BLOCK_SCALE+BLOCK_SCALE/10, 0, v);
		g.endShape();
	}
	
	public String toString(){
		return this.type.toString();
	}

}
